package com.aistar.controller;

import com.aistar.pojo.BigProduct;
import com.aistar.pojo.Product;
import com.aistar.pojo.ProductSpec;
import com.aistar.pojo.ProductVO;
import com.aistar.service.*;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductSpecService productSpecService;
    @Autowired
    BigProductService bigProductService;
    @Autowired
    ProductImagesService productImagesService;
    @Autowired
    SelectkeyService selectkeyService;
    @Autowired
    CommentService commentService;
    /**
     * 分页查询全部商品
     * @param request
     * @return
     */
    @RequestMapping("/product/allProduct")
    public ModelAndView pageAll(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        //获取url后面的页码
        String pageNumStr = request.getParameter("page");
        Integer pageNum = null;
        if (pageNumStr==null)
            pageNum=1;
        else
            pageNum = Integer.valueOf(pageNumStr);
        // 调用service

        PageInfo<Product> page = productService.getAllpage(pageNum);
        mav.addObject("page",page);
        mav.setViewName("/product/productList");
        mav.addObject("productList",page.getList());
        return mav;
    }
//
//    /**
//     * 查询单个商品
//     * @param productId
//     * @return
//     */
//    @GetMapping("/product/{productId}")
//    public ModelAndView getByProductId(@PathVariable("productId") Integer productId){
//        ModelAndView mav = new ModelAndView("/product/comment");
//        ProductVO productVO = null;
//        //根据id查询出商品
//        ServerResponse serverResponse = productService.getById(productId);
//        System.out.println(serverResponse);
//        if (serverResponse.getStatus()==1)
//            productVO = (ProductVO) serverResponse.getData();
////        mav.addObject("product",serverResponse.getData());
//        mav.addObject("product",productVO.getProduct());
//        mav.addObject("productImages",productVO.getProductImages());
//        return mav;
//    }

//    根据NAME查询
    @RequestMapping("/product/comment")
    public String getByProductName(String kw, HttpSession session){
        //根据id查询出商品
        ServerResponse serverResponse = productService.getByName(kw);
        System.out.println("测试测试测试测试测试");
        System.out.println(serverResponse);
        if (serverResponse.getStatus()==1) {
            Product  product= (Product) serverResponse.getData();
            session.setAttribute("product",product);
            ServerResponse serverResponse1 = productImagesService.getByProductIdTo(product.getProductId());
            if (serverResponse1.getStatus() == 1){
                session.setAttribute("productImages",serverResponse1.getData());
            }
            ServerResponse serverResponse2 = productSpecService.getByProId(product.getProductId());
            System.out.println(serverResponse2);
            if (serverResponse2.getStatus() == 1){
                ProductSpec productSpec = (ProductSpec) serverResponse2.getData();
                System.out.println(productSpec);
                session.setAttribute("productSpec",productSpec);
            }
        }
        return "product/comment";
    }
    /**
     * 分类查询商品
     * @param
     * @return
     */
    @GetMapping("/product/Big/{bigId}")
    public ModelAndView getByBigId(@PathVariable("bigId")String bigName){
        ModelAndView mav = new ModelAndView();
        //根据类别查询出所有细节类别
        List<BigProduct> bigProductList = bigProductService.getByBigName(bigName);
        mav.addObject("bigProductList",bigProductList);
        //根据BigId查询出所有商品
        List<Product> productList = new ArrayList<>();
        for (BigProduct b:bigProductList){
            List<Product> products = productService.getByBigId(b.getBigId());
            for (Product p:products){
                productList.add(p);
            }
        }
        //将所有商品以及商品评论以及商品细节放入List中
        List<ProductVO> productVOS = new ArrayList<>();
        for (Product p:productList){
            ServerResponse images = productImagesService.getByProductId(p.getProductId());
            productVOS.add(new ProductVO(p,String.valueOf(images)));
        }
        mav.addObject("productVOS",productVOS);
        mav.setViewName("product/productList");
        return mav;
    }

    @GetMapping("/productlist/select")
    public ModelAndView selectKey(HttpServletRequest request){

        String keyWords = request.getParameter("selectkey");
        List<ProductVO> productVOS = new ArrayList<>();
        List<Product> productList =  selectkeyService.selectByKey(keyWords);
        for (Product p:productList) {
            ServerResponse images = productImagesService.getByProductId(p.getProductId());
            productVOS.add(new ProductVO(p,String.valueOf(images)));
        }
        ModelAndView mav = new ModelAndView();
        mav.addObject("productVOS",productVOS);
        mav.setViewName("product/productList");
        return mav;
    }


}
